package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.ShopDao;
import com.aaa.book_mall.entity.Book;
import com.aaa.book_mall.entity.Shop;
import com.aaa.book_mall.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * fileName:ShopDaoImpl
 * description:
 * author:付亚星
 * createTime:2021/2/21 9:29
 * version:1.0.0
 */
@Repository
public class ShopDaoImpl implements ShopDao {

    //查询所有购物车数据
    @Override
    public List<Shop> showAllShop(Integer uid) {
        String sql = "select ts.uid,tb.bookname,ts.bookcount,ts.price,ts.bookprice from t_shop ts INNER JOIN t_book tb ON ts.bookid=tb.bookid where uid=?";
        List<List> lists = JDBCUtil.queryList(sql,uid);
        ArrayList<Shop> list1 = new ArrayList<>();
        if(lists.size()>0){
            for (List list : lists) {
                Shop shop = new Shop((Integer) list.get(0),(String)list.get(1),(Integer)list.get(2),(Integer)list.get(3),(Integer)list.get(4));
                //Shop shop=new Shop((Integer) list.get(0),(Integer) list.get(1),(Integer)list.get(2),(Integer)list.get(3),(Integer)list.get(4),(Integer)list.get(5));
                list1.add(shop);
            }
            return list1;
        }
        return null;
    }

    /**
     * 查询分页
     * @param shop
     * @return
     */
    @Override
    public int getShopCount(Shop shop) {
        StringBuilder sql = new StringBuilder("select count(*) from t_staff");
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            //因为是List<List>，所有两层数据，一层一层的获取，所以写两个get(0)
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    @Override
    public int insertShop(Shop shop) {
        String sql = "insert into t_shop values(null,?,?,?,?,?)";
        int update = JDBCUtil.update(sql,shop.getUid(),shop.getBookid(),shop.getBookcount(),shop.getPrice(),shop.getBookprice());
        return update;
    }



}
